package beans;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import javax.naming.NamingException;

import tools.DBManager;

public class ComponentList {
	
	private Connection conn = null;
	private Statement stmt = null;
	private ResultSet rs = null;
	private  ArrayList<ComponentBean> componentList= null;
	
	public ComponentList() 
	{
		componentList = new ArrayList<ComponentBean>();
		
		try{
			conn = DBManager.getConnection();
			stmt = DBManager.getStatement(conn);
			String sqlCmd = String.format("SELECT * FROM t_component");
			rs = stmt.executeQuery(sqlCmd);
			
			//fill the list
			while(rs.next())
			{
				ComponentBean cb= new ComponentBean();
				
				cb.setId(rs.getInt("ID"));
				cb.setName(rs.getString("Name"));
				cb.setQuantity(rs.getInt("Quantity"));
				
				cb.setComments(rs.getString("Comments") == null ? "" : rs.getString("Comments"));
				
				componentList.add(cb);
			}
		}catch (SQLException sqlException){
			
		} catch (NamingException namingException){
			
		} finally{
			DBManager.close(rs);
			DBManager.close(stmt);
			DBManager.close(conn);
		}		
	}
	
	public ArrayList<ComponentBean> getComponents()
	{
		return componentList;
	}
}
